<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Weather {

    var $location;
    var $day;
    var $min;
    var $max;
    var $img;
    var $cur;
    var $hmd;
    var $pre;

    function Weather($loc = NULL) {
        if ($loc == NULL)
            return FALSE;

        $this->location = $loc['loc'];
    }

    function set_location($location) {
        $this->location = $location;
    }

    function initialize() {

        $weather_feed = file_get_contents("http://weather.yahooapis.com/forecastrss?w=" . $this->location . "&u=c");
        $weather = simplexml_load_string($weather_feed);
        if (!$weather)
            die('weather failed');
        $copyright = $weather->channel->copyright;

        $channel_yweather = $weather->channel->children("http://xml.weather.yahoo.com/ns/rss/1.0");

        foreach ($channel_yweather as $x => $channel_item) {
            //echo $x.":".$channel_item.'<br />';
            foreach ($channel_item->attributes() as $k => $attr) {
                //echo '-->'.$k.":".$attr.'<br />';
                $yw_channel[$x][$k] = $attr;
            }
        }

        $item_yweather = $weather->channel->item->children("http://xml.weather.yahoo.com/ns/rss/1.0");

        foreach ($item_yweather as $x => $yw_item) {
            //echo $x.":".$yw_item.'<br />';
            foreach ($yw_item->attributes() as $k => $attr) {
                //echo '-->'.$k.":".$attr.'<br />';
                if ($k == 'day')
                    $day = $attr;
                if ($x == 'forecast') {
                    $yw_forecast[$x][$day . ''][$k] = $attr;
                } else {
                    $yw_forecast[$x][$k] = $attr;
                }
            }
        }

        //var_dump($yw_forecast);
        $this->img = $this->getImage();
        $this->day = date('D');
        $this->min = $yw_forecast['forecast'][date('D')]['low'];
        $this->max = $yw_forecast['forecast'][date('D')]['high'];
        $this->cur = $yw_forecast['condition']['temp'];
        $this->hmd = $yw_channel['atmosphere']['humidity'];
        $this->pre = $yw_channel['atmosphere']['pressure'];
    }

    function getImage() {

        //I am using the DOM(Document Object Model) library to <span class="posthilit">read</span> the entire XML document into memory first.
        $doc = new DOMDocument();
        $doc->load("http://weather.yahooapis.com/forecastrss?w=" . $this->location . "&u=c");
        //now I get all elements inside this document with the following name "channel", this is the 'root'
        $channel = $doc->getElementsByTagName("channel");
        //now I go through each item withing $channel
        foreach ($channel as $chnl) {
            //I then find the 'item' element inside that loop
            $item = $chnl->getElementsByTagName("item");
            foreach ($item as $itemgotten) {
                //now I search within '$item' for the element "description"
                $describe = $itemgotten->getElementsByTagName("description");
                //once I find it I create a variable named "$description" and assign the value of the Element to it
                $description = $describe->item(0)->nodeValue;
                //and display it on-screen

                $img = explode('<br />', $description);
                return $img[0];
            }
        }
    }

    function img() {
        return $this->img;
    }

    function day() {
        return $this->day;
    }

    function tmin() {
        return $this->min;
    }

    function tmax() {
        return $this->max;
    }

    function cur() {
        return $this->cur;
    }

    function hmd() {
        return $this->hmd;
    }

    function pre() {
        return $this->pre;
    }

}

?>